<div class="page-article" id="J_DataTable">
	<div class="member_welhead"><ins></ins><b><{t}>集货单详情<{/t}></b> </div>
	<div id="member_notify" class="member-messagebox">
		<div class="member-mod">
			<div style="height: 40px;line-height: 40px;font-size: 15px; padding-left:10px;"><b><{t}>集货单号：<{/t}><{$gc_id}></b> </div>
			<input type="hidden" name="mainOrder_id" value="<{$mainOrder_id}>">
			<input type="hidden" name="gc_id" value="<{$gc_id}>">
			<input type="hidden" id="opt" name="opt" value="">
			
			<{include file="site/member/gc_list.html" app="b2c"}>
		</div>
		<{pagers data=$pager}>
	</div>
</div>

<script>
var item = $$('#J_DataTable input[name^=item[]')||[];
var all = $$('#J_DataTable .all-selector')||[];
var confirm = $('J_DataTable');

//没有选择商品时，按钮为灰色
window.addEvent('domready',function(){
	$$('.selector').addEvent('click',function(e){
		if(show().length==0){
			no_checked();
		}else{
			has_checked();
		}
	});
});

function do_select(){
	if(show().length==0){
		no_checked();
	}else{
		has_checked();
	}
}

confirm.addEvents({
	'click:relay(.single_confirm)':function(e){
		e.preventDefault();
		var self = this;
		var input = this.getParent('td').getPrevious('td').getElement('.toShare');
		var num = input.get('value');
		var quantity = this.getParent('td').getPrevious('td').getPrevious('td').getElement('.quantity').get('value');
		if(num && num >= 0){
		    if(num > quantity){
			    var tips = new Tips(input);
		        tips.show('<{t}>实际集货数量不大于计划数量！<{/t}>');
				return false;
			}
			Dialog.confirm('<{t}>对此商品集货？<{/t}>', function(e){
				if(e) {
					 new Request({
						url:self.href,
						data:'con_num=' + num + '&mainOrder_id=<{$mainOrder_id}>&gc_id=<{$gc_id}>',
						method:'post',
						onSuccess:function(response){
						    var res = JSON.decode(response);
							if (res.error){
								Message.error(res.error);
							}else{
								if (!res.data&&res.reload != null){
									location.href=res.reload;
								}else{
									var js='';
									var html = res.data.stripScripts(function(script){
										js = script;
									});

									$('switchable').innerHTML = html;
									Browser.exec(js);
									return;
								}
							}
						}
					}).send();	
				}
			});
		}else{
		    var tips = new Tips(input);
		    tips.show('<{t}>实际集货数不能小于0！<{/t}>');
			return false;
		}
	}
});

//选中商品的状态为黄色
function has_checked(){
	$('confirm_batch').set('onclick','javascript:goods_confirm("batch");');
	$('confirm_batch').setStyle('background-color','#f18007');
	//$('confirm_final').set('onclick','javascript:goods_confirm("final");');
	//$('confirm_final').setStyle('background-color','#f18007');
}

//不选中时为灰色
function no_checked(){
	$('confirm_batch').set('onclick','javascript:;');
	$('confirm_batch').setStyle('background-color','gray');
	//$('confirm_final').set('onclick','javascript:;');
	//$('confirm_final').setStyle('background-color','gray');
	all.removeProperty('checked');
}

//全选
function selectAll(obj){
	if(obj.checked){
	    has_checked();
	}else{
	    no_checked();
	}
	var arr=document.getElementsByName("item[]");
	for(var i=0;i<arr.length;i++){
		arr[i].checked = obj.checked;
	}
}

function isSelected(){
	item = $$('#J_DataTable input[name^=item[]')||[];
	if(!item) return false;
	for(i=0;i<item.length;i++){
		if(item[i].checked){
			return true;
		}
	}
	all = $$('#J_DataTable .all-selector')||[];
	if(!all) return false;
	for(i=0;i<all.length;i++){
		if(all[i].checked){
			return true;
		}
	}
	return false;
}


//获取选中商品
function show(){
	var cBox = document.getElementsByName("item[]");
	var idents = new Array();
	for(i = 0;i < cBox.length;i++){
		if(cBox[i].checked){
			idents.push(cBox[i].value);
		}
	}
	return idents;
}


function goods_confirm(opt){
	if(opt != 'final' && !isSelected()){
		Message.error('<{t}>请选择商品！<{/t}>');
        return false;
	}
    $('opt').set('value',opt);
    
	var cBox = document.getElementsByName("item[]");
	for(i = 0;i < cBox.length;i++){
	    if(cBox[i].checked){
		    var input = document.getElementById("con_num["+cBox[i].value+"]");
			var num = input.get('value');
			var quantity = input.getParent('td').getPrevious('td').getElement('.quantity').get('value');
			if(!(num && num >= 0)){
				var tips = new Tips(input);
				tips.show('<{t}>实际集货数不能小于0！<{/t}>');
				return false;
			}
			if(num > quantity){
			    var tips = new Tips(input);
		        tips.show('<{t}>实际集货数量不大于计划数量！<{/t}>');
				return false;
			}
		}
	}

	var title = '<{t}>是否对所选商品集货？<{/t}>';
	if(opt == 'final')
	{
		title = '<{t}>是否确认完成？<{/t}>';
	}
	Dialog.confirm(title,function(e){
		if(!e) return false;
		new Request({
			url:'<{link app=b2c ctl=site_member act=do_goodscollection_edit}>',
			method:'post',
			secure:false,
			data:$('J_DataTable'),
			onSuccess:function(response){
				var res = JSON.decode(response);
				if (res.error){
					Message.error(res.error);
				}else{
					if (!res.data&&res.reload != null){
						location.href=res.reload;
					}else{
						var js='';
						var html = res.data.stripScripts(function(script){
							js = script;
						});
						$('switchable').innerHTML = html;
						Browser.exec(js);
						return;
					}
				}
			}
		}).post();
		return false;
	});
}

</script>
